var ContactSlider = new Class({
	Implements: [Events, Options],

	options: {
		changeSlideDuration: 1200,
		intervalSlideDuration: 7000,
		intervalArrowDuration: 5000,
		changeArrowDuration: 500
	},

	initialize: function(options){
		if($$('#contact_me').length == 0)
			return;

		this.setOptions(options);

		this.container = $('contact_me');
		this.arrow = this.container.getElements('.scroll .arrow')[0];
		this.timer = null;

		this.items = this.container.getElements('.item');
		this.nbItems = this.items.length;

		if(this.nbItems < 2)
			return;

		this.currentItem = 0;
		this.build();
		this.enable();
	},

	build: function(){
		this.items.each(function(item, index){
			item.store('fx', new Fx.Morph(item, {
				link: 'cancel',
				duration: this.options.changeSlideDuration
			}));
		}.bind(this));

		setInterval(function(){
			this.arrowMove();
		}.bind(this), this.options.intervalArrowDuration);

		this.arrow.store('start', this.arrow.getStyle('margin-top').toInt());
		this.arrow.store('end', this.arrow.retrieve('start')+20);

		this.arrow.store('fx', new Fx.Morph(this.arrow, {
			link: 'cancel',
			duration: this.options.changeArrowDuration
		}));
	},

	arrowMove: function(){
		this.arrow.retrieve('fx').start({
			'margin-top': this.arrow.retrieve('end')
		}).chain(function(){
			this.arrow.retrieve('fx').start({
				'margin-top': this.arrow.retrieve('start')
			});
		}.bind(this));
	},

	next: function(){
		this.show((this.currentItem+1)%this.nbItems);
	},

	show: function(index){
		var itemOld = this.items[this.currentItem];
		this.currentItem = index;
		var itemNew = this.items[this.currentItem];

		itemNew.setStyles({
			'z-index' : 2,
			'left' : 0
		});

		itemNew.retrieve('fx').start({
			'width': itemNew.getElements('img')[isDesktop() ? 0 : 1].getSize().x
		}).chain(function(){
			itemNew.setStyles({
				'z-index' : 1,
				'width' : '100%'
			});
			itemOld.setStyle('width', 0);
		}.bind(this));
	},

	enable: function(){
		this.timer = setInterval(function(){
			this.next();
		}.bind(this), this.options.intervalSlideDuration);
	}
});

var ContactFeed = Class({
	maxWidth:350,
	minWidth:150,
	initialize : function(){
		if($$('#contact_feed').length == 0)
			return;

		this.container = $$('div#contact_feed')[0];
		this.items = this.container.getElements('.item');
		this.item_width = this.items[0].getWidth();
		this.item_height = this.items[0].getHeight();

		this.applyResize(true);
		this.initializeEvents();
	},

	applyResize : function(load){
		var fix = 0;
		if(load == true)
			fix = 17;
		var width = window.getSize().x-fix;

		var itemDimensions = 0;
		var items = this.items;

		for (var i = 1; i < items.length; i++)
		{
			var ratio = Math.floor(width / i);
			if (ratio >= this.minWidth && ratio <= this.maxWidth ){
				itemDimensions = ratio;
				break;
			}
		}

		if(itemDimensions == 0 && width < this.minWidth)
			itemDimensions = width;
		else if(itemDimensions == 0)
			itemDimensions = this.maxWidth;

		this.items.setStyles({'width' : itemDimensions, 'height' : itemDimensions*4/5});
	},

	initializeEvents : function(){
		window.addEvent('resize', function(){
			this.applyResize();
		}.bind(this));
	}
});
